import 'babel-polyfill'
import router from './router'
import App from './App'
import store from './store'
import * as Constants from './utils/constants'
import Utils from 'monkey-ui/lib/utils/util'
import './utils/prototype'
import Vue2Filters from 'vue2-filters'
import NProgress from 'vue-nprogress'
import './directives'
import VueClipboards from 'vue-clipboards';


Vue.config.productionTip = false
Vue.use(Utils)
Vue.use(Vue2Filters)
Vue.use(NProgress)
Vue.use(VueClipboards)

import {
  OrganizationChart,
  AuthorityList,
  AuthorityAdd,
  Organization,
  Breadcrumb,
  Menu,
  IsLoading,
  Ckeditor,
  ObjectSelect,
  SearchComponent
} from 'monkey-ui'

Vue.use(OrganizationChart)
Vue.use(AuthorityList)
Vue.use(AuthorityAdd)
Vue.use(Organization)
Vue.use(Breadcrumb)
Vue.use(Menu)
Vue.use(IsLoading)
Vue.use(SearchComponent)
Vue.use(Ckeditor)
Vue.use(ObjectSelect)

Vue.http.options.emulateJSON = true;
Vue.http.interceptors.push(function (request, next) {
  request.headers.set('X-Requested-With', 'XMLHttpRequest');
  if (request.params) {
    request.params.t = new Date().getTime();
  }
  next(function (response) {
    let data = response.body;
    if (data.responseCode === Constants.OAUTH2_TOKEN_INVALID_ERROR || data.responseCode === Constants.OAUTH2_NOT_LOGIN_ERROR) {
      window.location.href = Constants.LOGIN_URL;
      response.ok = false;
      return response;
    }

    if (data.responseCode === Constants.OAUTH2_KICKOUT_SESSION_ERROR) {
      ELEMENT.MessageBox.alert(data.message, '提示', {
        confirmButtonText: '确定',
        callback: action => {
          window.location.href = Constants.LOGIN_URL;
        }
      });
      response.ok = false;
      return response;
    }

    if (data.responseCode !== Constants.OPERATE_SUCCESS) {
      ELEMENT.Message.warning(data.message);
      response.ok = false;
      response.status = 403;
      return response;
    }

  });
});

const nprogress = new NProgress({parent: '.nprogress-container', showSpinner: false})


/* eslint-disable no-new */
new Vue({
  el: '#app',
  router,
  store,
  nprogress,
  template: '<App/>',
  components: {App}
})

